package com.lostreseditores.sabersai.dataaccess.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import com.lostreseditores.sabersai.dataaccess.sessionFactory.HibernateSessionFactory;
import com.lostreseditores.sabersai.model.Acceso;


public class AccesoDAO implements IAccesoDAO {
    private static final Log log = LogFactory.getLog(AccesoDAO.class);

    // property constants
    //public static final Integer  ACCEIDN = "acceidn";
    public static final String ACCEIDN = "acceidn";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            Acceso Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(Acceso instance) {
        log.debug("saving Acceso instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            Acceso Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(Acceso instance) {
        log.debug("deleting Acceso instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            Acceso Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(Acceso instance) {
        log.debug("updating Acceso instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public Acceso findById(Integer id) {
        log.debug("finding Acceso instance with id: " + id);

        try {
            Acceso instance = (Acceso) getSession().get(Acceso.class, id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding Acceso failed", re);
            throw re;
        }
    }

    public List<Acceso> findByExample(Acceso instance) {
        log.debug("finding Acceso instance by example");

        try {
            List results = getSession().createCriteria("Acceso")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  Acceso entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< Acceso> found by query
        */
    public List<Acceso> findByProperty(String propertyName, Object value) {
        log.debug("finding Acceso instance with property: " + propertyName +
            ", value: " + value);

        try {
            String queryString = "from Acceso as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<Acceso> findByAcceidn(Object acceidn) {
        return findByProperty(ACCEIDN, acceidn);
    }

    /**
    * Find all Acceso entities.
    *
    * @return List<Acceso> all Acceso instances
    */
    public List<Acceso> findAll() {
        log.debug("finding all Acceso instances");

        try {
            String queryString = "from Acceso";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<Acceso> findByCriteria(String whereCondition) {
        log.debug("finding Acceso " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from Acceso model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<Acceso> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in Acceso failed", re);
            throw re;
        }
    }

    public List<Acceso> findPageAcceso(String sortColumnName,
        boolean sortAscending, int startRow, int maxResults) {
        log.debug("finding Acceso findPageAcceso");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from Acceso model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from Acceso model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberAcceso() {
        log.debug("finding Acceso count");

        try {
            String queryString = "select count(*) from Acceso model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
}
